(function() {
	var $optimize = $('div#optimize');
	var $items = $('div.items', $optimize);
	window.$dialog = $("div.optimize-dialog", $optimize);
	
	window.$dialog.dialog({
		autoOpen: false,
		modal: true,
		width: 900,
		open: function(event, ui) {
			;
		},
		close: function(event, ui){
			;
		}
	});
	
	var $keyword = $("#keyword");
	$keyword.css('color', '#666');
	$keyword.focus(function() {
		if ($(this).val() == '关键字') {
			$(this).css('color', 'black');
			$(this).val('');
		}
	});
	$keyword.blur(function() {
		if ($(this).val() == '') {
			$(this).val('关键字');
			$(this).css('color', '#666');
		}
	});

	$("#search").button().click(function(){
		try
		{
			var filter = getFilterParameters();
			$("div.items", $optimize).data('filter', filter);
			loadPage(1);
		}
		catch(error)
		{
			alert(e);
		}
		return false;
	});

	$("#search-form").submit(function()
	{
		return false;
	});

	function getFilterParameters()
	{
		var filter = {};
		var $form = $("#search-form");
		var saleStatus = $("select[name='filter.saleStatus']", $form).val();
		var banner = $("select[name='filter.saleStatus'] option:selected", $form).attr('banner');
		filter['filter.saleStatus'] = saleStatus;
		filter['filter.banner'] = banner;
		var cids = null;
		var c = $("select[name='filter.sellerCids']", $form);
		var cid = c.val();
		if (cid)
		{
			cids = [cid];
			$("option[parent='" + cid + "']", $form).each(function(){
				cids.push($(this).val());
			});
		}
		var keyword = $("input[name='filter.keyWord']", $form).val();
		if (keyword == '关键字')
		{
			keyword = null;
		}
		
		if (cids)
		{
			filter['filter.sellerCids'] = cids.join(',');
		}
		if (keyword)
		{
			filter['filter.keyWord'] = keyword;
		}
		return filter;
	}

	$(".pager", $optimize).pager({ 
		pagenumber: 1, 
		pagecount: $('table#optimize-items').attr('pages'), 
		buttonClickCallback: loadPage, 
		firstLabel: "首页", 
		prevLabel: "前一页", 
		nextLabel: "下一页", 
		lastLabel: "末页" 
	});

	function loadPage(number, callback) {
	    var limit = $('table#optimize-items').attr('page-size');
	    var offset = (number - 1) * limit;
	    var filter = $items.data('filter');
	    var pagingOption = {};
	    pagingOption['option.limit'] = limit;
	    pagingOption['option.offset'] = offset;
	    var data = $.extend({}, filter, pagingOption);
	    $("div.items").html("<img src='images/loading.gif'/>");
		$.ajax({
			url: 'items',
			data: data,
			type: 'POST',
			success: function(data) {
				$items.html(data);
				//checkSelection();
				var pageCount = parseInt($("table#optimize-items").attr("pages"));
				$(".pager", $optimize).pager({ 
					pagenumber: number, 
					pagecount: pageCount, 
					buttonClickCallback: loadPage, 
					firstLabel: "首页", 
					prevLabel: "前一页", 
					nextLabel: "下一页", 
					lastLabel: "末页" 
				});
				if (callback)
				{
					callback();
				}
			}
		});
	}

	window.optimize = {};
	window.optimize.reload = function(callback){
		var i = parseInt($("table#optimize-items").attr("pageIndex"));
		loadPage(i+1, callback);
	};

})();